Assignment-21

Author

Aldair Perez Marmolejo

library(dataRetrieval)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(lubridate)
library(tidyr)
library(tibble)
library(tsibble)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'

The following object is masked from 'package:lubridate':

    interval

The following objects are masked from 'package:base':

    intersect, setdiff, union
library(ggplot2)
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(feasts)
Loading required package: fabletools
library(fabletools)
library(zoo)

Attaching package: 'zoo'

The following object is masked from 'package:tsibble':

    index

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric
poudre_flow <- readNWISdv(siteNumber = "06752260",
                          parameterCd = "00060",
                          startDate = "2013-01-01",
                          endDate = "2023-12-31") |>
  renameNWISColumns() |>
  mutate(Date = floor_date(Date, "month")) |>
  group_by(Date) |>
  summarise(Flow = mean(Flow))  
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
poudre_flow_tsibble <- poudre_flow |>
  as_tsibble(index = Date)
p_river_plot <- ggplot(poudre_flow_tsibble, aes(x = Date, y = Flow)) +
  geom_line(color = "red") +
  labs(title = "Monthly Flow Time Series", x = "Date", y = "Flow (cfs)") +
  theme_minimal()
print(p_river_plot)

animated_plot <- ggplotly(p_river_plot, dynamicTicks = TRUE)
animated_plot
poudre_flow_tsibble <- poudre_flow |>
  as_tsibble(index = Date) |>
  fill_gaps()

gg_subseries(poudre_flow_tsibble, Flow, na.rm = TRUE) +
  labs(title = "Subseries of Monthly Flow in Poudre River",
       y = "Flow (cfs)")
Warning: Removed 3746 rows containing missing values or values outside the scale range
(`geom_hline()`).

The seasons are defined according to the date (certain months/dates go to their respective seasons). Subseries represents group a particular series, like a dataset, into specific groups by category, such as season.

full_date_seq <- seq.Date(from = as.Date("2013-01-01"), to = as.Date("2023-12-31"), by = "month")

poudre_flow_tsibble <- poudre_flow |>
  as_tsibble(index = Date) |>
  right_join(tibble(Date = full_date_seq), by = "Date") |>
  fill_gaps(.full = TRUE) |>
  mutate(Flow = zoo::na.approx(Flow, na.rm = FALSE))

decomposed <- poudre_flow_tsibble |>
  model(STL = STL(Flow ~ season(window = 12) + trend(window = 13)))

decomposed |>
  components() |>
  autoplot() +
  labs(title = "STL Decomposition of Monthly Flow in Poudre River")

I see 5 graphs for flow, trend, season_year, season_week, and remainder. There are several flucuations in the data, mostly in 2014-2016 as well as 2021 - 2023. While Trend represents the general movement of water flow, the seasons represent the 12-month representation of how much water flows in each month/week.